/*
    Project Milestone 3
    by Lorenzo Miori [Lorenzo.Miori@stud-inf.unibz.it,
    Matteo Angriman [Matteo.Angriman@stud-inf.unibz.it],
    Nick Preda [Nicolae.Preda@stud-inf.unibz.it]
*/

package GUI;

import DB.AddressDB;
import DB.DatabaseLogin;
import DB.HotelDB;
import java.util.HashMap;

/**
 *
 * @author lorenzo
 */
public class ManageHotelGUI extends BasicManageGUI {

    /**
     * Creates new form ManageHotelGUI
     * defaultEntry can be null when "adding new data to the DB"
     */
    public ManageHotelGUI(DataModifiedListener changeListener, HashMap<String, String> defaultEntry) {
        super(changeListener, defaultEntry);
        initComponents();
        setTitle("Hotel Entry", "Edit hotel's details");
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        mainAreaPanel.add(fieldPanel);
        fillDataFields();
        finalSetup();
    };
    
    @Override
    protected void refreshCurrentData() {
        HashMap<String, String> currentEntry = getCurrentData();
        /* ID stays the same */
        currentEntry.put(HotelDB.HOTEL_NAME, nameFieldText.getText());
        currentEntry.put(HotelDB.HOTEL_ADDRESS, ""+addressComboBox.getSelectedIndex());
        currentEntry.put(HotelDB.HOTEL_STARS_RATE, ratingFieldText.getValue()+"");
        
    }
    
    private void fillDataFields() {
        
        addressComboBox.removeAllItems();
        HashMap<String, String> currentEntry = getCurrentData();
        if (currentEntry == null) {
            return;
        }
        /* Fill the drop menu */
        int addrIndex = 0;
        int i = 0;
        for (HashMap<String, String> addr : new AddressDB(DatabaseLogin.getLastConnection()).getTableData() ) {
            addressComboBox.addItem(AddressDB.formatAddress(addr));
            if (addr.get(AddressDB.ADDRESS_ID).compareTo(nullSentinel(currentEntry.get(HotelDB.HOTEL_ADDRESS))) == 0)
                addrIndex = i;
            i++;
        }
        
        /* NOTE: if this is a new entry, value may be null!! */
        uniqueIDLabel.setText("ID: "+nullSentinel(currentEntry.get(HotelDB.HOTEL_ID)));
        nameFieldText.setText(nullSentinel(currentEntry.get(HotelDB.HOTEL_NAME)));
        ratingFieldText.setValue(Integer.parseInt(
                                    nullSentinelInteger(
                                    currentEntry.get(HotelDB.HOTEL_STARS_RATE))));
        addressComboBox.setSelectedIndex(addrIndex);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fieldPanel = new javax.swing.JPanel();
        ratingFieldText = new javax.swing.JSlider();
        ratingLabel = new javax.swing.JLabel();
        uniqueIDLabel = new javax.swing.JLabel();
        addAddressButton = new javax.swing.JButton();
        addressComboBox = new javax.swing.JComboBox();
        nameLabel = new javax.swing.JLabel();
        addressLabel = new javax.swing.JLabel();
        nameFieldText = new javax.swing.JTextField();

        setTitle("Hotel entry");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setPreferredSize(new java.awt.Dimension(600, 400));

        ratingFieldText.setMaximum(10);
        ratingFieldText.setMinimum(1);
        ratingFieldText.setSnapToTicks(true);

        ratingLabel.setText("Stars");

        uniqueIDLabel.setText("ID: ?");

        addAddressButton.setText("Add...");
        addAddressButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAddressButtonActionPerformed(evt);
            }
        });

        addressComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        nameLabel.setText("Name");

        addressLabel.setText("Address");

        nameFieldText.setText("jTextField1");

        javax.swing.GroupLayout fieldPanelLayout = new javax.swing.GroupLayout(fieldPanel);
        fieldPanel.setLayout(fieldPanelLayout);
        fieldPanelLayout.setHorizontalGroup(
            fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 531, Short.MAX_VALUE)
            .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(fieldPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(ratingLabel)
                        .addComponent(uniqueIDLabel)
                        .addComponent(addressLabel)
                        .addComponent(nameLabel))
                    .addGap(37, 37, 37)
                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(ratingFieldText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(nameFieldText, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE))
                        .addComponent(addressComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(addAddressButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(68, Short.MAX_VALUE)))
        );
        fieldPanelLayout.setVerticalGroup(
            fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
            .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(fieldPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(fieldPanelLayout.createSequentialGroup()
                            .addGap(57, 57, 57)
                            .addComponent(addAddressButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, fieldPanelLayout.createSequentialGroup()
                            .addComponent(uniqueIDLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(nameLabel)
                                .addComponent(nameFieldText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(10, 10, 10)
                            .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(addressComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(addressLabel))))
                    .addGroup(fieldPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(fieldPanelLayout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(ratingFieldText, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(fieldPanelLayout.createSequentialGroup()
                            .addGap(37, 37, 37)
                            .addComponent(ratingLabel)))
                    .addContainerGap()))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fieldPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(fieldPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 143, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addAddressButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAddressButtonActionPerformed
        setEnabled(false);
        new AddressListGUI(this);
    }//GEN-LAST:event_addAddressButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAddressButton;
    private javax.swing.JComboBox addressComboBox;
    private javax.swing.JLabel addressLabel;
    private javax.swing.JPanel fieldPanel;
    private javax.swing.JTextField nameFieldText;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JSlider ratingFieldText;
    private javax.swing.JLabel ratingLabel;
    private javax.swing.JLabel uniqueIDLabel;
    // End of variables declaration//GEN-END:variables

}
